package com.sc.nft.task;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.sc.nft.dao.*;
import com.sc.nft.entity.*;
import com.sc.nft.enums.AllocationTypeEnum;
import com.sc.nft.enums.AwardsTypeEnum;
import com.sc.nft.service.*;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
@Slf4j
public class IncomeTask {

    private final UserInterestsRedPacketService userInterestsRedPacketService;
    private final UserCityServerCentreService userCityServerCentreService;
    private final DaoShareConfigDao daoShareConfigDao;
    private final IncomeService incomeService;
    private final DaoMoneyPoolDao daoMoneyPoolDao;
    private final SharePoolWeekService sharePoolWeekService;
    private final EquityProfitRuleDao equityProfitRuleDao;
    private final EquityProfitRuleService equityProfitRuleService;
    private final EquitySharePoolDao equitySharePoolDao;

    /**
     * 计算道具分红
     */
    @XxlJob("calPropsShare")
    public void calPropsShare() {
        List<EquityProfitRule> equityProfitRules = equityProfitRuleDao.findByStatus(true);
        List<EquitySharePool> list = equitySharePoolDao.list();
        for (EquityProfitRule equityProfitRule : equityProfitRules) {
            equityProfitRuleService.calculateEquityProfit(equityProfitRule, list);
        }
    }




    /**
     * 计算本周分红奖励
     */
    @XxlJob("calSharePoolWeek")
    public void calSharePoolWeek() {
        DateTime thisWeek = DateUtil.beginOfWeek(DateTime.now());
        String yearWeek = String.valueOf(thisWeek.getField(DateField.YEAR)) + String.valueOf(DateUtil.weekOfYear(thisWeek));
        List<DaoMoneyPool> waitSettle = daoMoneyPoolDao.getWaitSettle(yearWeek, AllocationTypeEnum.BOUNDS_POOL);
        waitSettle.forEach(sharePoolWeekService::calShareMoney);
    }

    /**
     * 培育奖
     */
    @XxlJob("cultivationAward")
    public void cultivationAward() {
        DateTime thisWeek = DateUtil.beginOfWeek(DateTime.now());
        String yearWeek = String.valueOf(thisWeek.getField(DateField.YEAR)) + String.valueOf(DateUtil.weekOfYear(thisWeek));
        List<DaoShareConfig> daoShareConfigs = daoShareConfigDao.getByOpenType(AwardsTypeEnum.CULTIVATION_AWARD);
        for (DaoShareConfig daoShareConfig : daoShareConfigs) {
            incomeService.cultivationAward(daoShareConfig, yearWeek);
        }
    }

    /**
     * 组织贡献奖
     */
    @XxlJob("contributionAward")
    public void contributionAward() {
        DateTime nextWeek = DateUtil.beginOfWeek(DateTime.now());
        String yearWeek = String.valueOf(nextWeek.getField(DateField.YEAR)) + String.valueOf(DateUtil.weekOfYear(nextWeek));
        List<DaoShareConfig> daoShareConfigs = daoShareConfigDao.getByOpenType(AwardsTypeEnum.CONTRIBUTION_AWARD);
        for (DaoShareConfig daoShareConfig : daoShareConfigs) {
            incomeService.contributionAward(daoShareConfig, yearWeek);
        }
    }

    /**
     * 服务中心分红
     *
     * @throws Exception
     */
    @XxlJob("serviceCenterDividends")
    public void serviceCenterDividends() throws Exception {
        DateTime nextWeek = DateUtil.beginOfWeek(DateTime.now());
        String yearWeek = String.valueOf(nextWeek.getField(DateField.YEAR)) + String.valueOf(DateUtil.weekOfYear(nextWeek));
        List<DaoShareConfig> daoShareConfigs = daoShareConfigDao.getByOpenType(AwardsTypeEnum.SERVICE_CENTER_DIVIDENDS);
        for (DaoShareConfig daoShareConfig : daoShareConfigs) {
            incomeService.serviceCenterDividends(daoShareConfig, yearWeek);
        }
    }


    /**
     * 服务中心平级
     *
     * @throws Exception
     */
    @XxlJob("serviceCenterLevel")
    public void serviceCenterLevel() throws Exception {
        DateTime nextWeek = DateUtil.beginOfWeek(DateTime.now());
        String yearWeek = String.valueOf(nextWeek.getField(DateField.YEAR)) + String.valueOf(DateUtil.weekOfYear(nextWeek));
        List<DaoShareConfig> daoShareConfigs = daoShareConfigDao.getByOpenType(AwardsTypeEnum.SERVICE_CENTER_LEVEL);
        for (DaoShareConfig daoShareConfig : daoShareConfigs) {
            incomeService.serviceCenterLevel(daoShareConfig, yearWeek);
        }
    }

    /**
     * 社区网点分红
     *
     * @throws Exception
     */
    @XxlJob("workRoomDividends")
    public void workRoomDividends() throws Exception {
        DateTime nextWeek = DateUtil.beginOfWeek(DateTime.now());
        String yearWeek = String.valueOf(nextWeek.getField(DateField.YEAR)) + String.valueOf(DateUtil.weekOfYear(nextWeek));
        List<DaoShareConfig> daoShareConfigs = daoShareConfigDao.getByOpenType(AwardsTypeEnum.WORK_ROOM_DIVIDENDS);
        for (DaoShareConfig daoShareConfig : daoShareConfigs) {
            incomeService.workRoomDividends(daoShareConfig, yearWeek);
        }
    }


    /**
     * 持仓分红
     *
     * @throws Exception
     */
    @XxlJob("positionReward")
    public void positionReward() throws Exception {
        DateTime nextWeek = DateUtil.beginOfWeek(DateTime.now());
        String yearWeek = nextWeek.getField(DateField.YEAR) + String.valueOf(DateUtil.weekOfYear(nextWeek));
        List<DaoShareConfig> daoShareConfigs = daoShareConfigDao.getByOpenType(AwardsTypeEnum.POSITION_REWARD);
        for (DaoShareConfig daoShareConfig : daoShareConfigs) {
            incomeService.positionReward(daoShareConfig, yearWeek);
        }
    }

}
